package cn.bluech.template.ui;

import android.Manifest;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import cn.bluech.template.global.Route;
import cn.bluech.template.ui.base.BaseActivity;


/**
 * Description:
 * Create by Codeant on 2019/1/19 0019
 */
public class SplashActivity extends BaseActivity {
    private static final String TAG  = SplashActivity.class.getSimpleName();
    private static String[] PERMISSIONS = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE};


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_splash);
//        ButterKnife.bind(this);


        if(hasAllPermission())
            entry();
        else
            requestPermissions();
    }

    private void entry(){
        Route.j2MainActivity(this);
        finish();
    }

    private boolean hasAllPermission(){
        for(String permission:PERMISSIONS){
            if(ContextCompat.checkSelfPermission(this,permission)== PackageManager.PERMISSION_DENIED)
                return false;
        }

        return true;
    }

    private void requestPermissions(){
        ActivityCompat.requestPermissions(this,PERMISSIONS,0);
    }


    private void showDeniedDialog(){
        new AlertDialog.Builder(this)
                .setTitle("权限申请")
                .setMessage("未获得应用运行所需的基本权限，请在设置中开启权限后再使用！")
                .setCancelable(false)
                .setPositiveButton("确认", (DialogInterface.OnClickListener) (dialog, which) ->
                        finish()
                )
                .create()
                .show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        for(int grantResult:grantResults){
            if(grantResult==PackageManager.PERMISSION_DENIED){
                showDeniedDialog();
                return;
            }
        }

        entry();
    }

    @Override
    public void onBackPressed() {
        // 禁止返回键
    }
}
